<?php

namespace app\platform\controller\terminal;

use app\BaseController;

use app\common\model\ShopDetailModel;



class ShopDetail extends BaseController{

    public function show()

    {

        $shop_detail=ShopDetailModel::find($this->request->shopId);

        if(null === $shop_detail){

            $shop_detail = new ShopDetailModel();

            $shop_detail->shop_id=$this->request->shopId;

            $shop_detail->save();

        }

        $detail=[

            'name'=>$this->request->shop->name,

            'logo'=>$shop_detail !== null ? $shop_detail->logo :'',

            'door_license_plate'=>$shop_detail !== null ? $shop_detail->door_license_plate :'',

            'business_hours'=>$shop_detail !== null ? $shop_detail->business_hours :'',

            'tel'=>$shop_detail !== null ? $shop_detail->tel :'',

            'address'=>$shop_detail !== null ? $shop_detail->address :'',

            'info'=>$shop_detail !== null ? $shop_detail->info :'',

            'lng'=>$shop_detail !== null ? $shop_detail->lng :'',

            'lat'=>$shop_detail !== null ? $shop_detail->lat :'',

        ];

        return $this->success(['detail'=>$detail]);



    }



    public function save()

    {

        $name=$this->request->param('name');

        $logo=$this->request->param('logo');

        $door_license_plate=$this->request->param('door_license_plate');

        $business_hours=$this->request->param('business_hours');

        $tel=$this->request->param('tel');

        $address=$this->request->param('address');

        $info=$this->request->param('info');

        $lng=$this->request->param('lng');

        $lat=$this->request->param('lat');

        if(empty($name)){

            return $this->error('名称必须填写');

        }

        if(empty($logo)){

            return $this->error('请上传logo');

        }

        if(empty($door_license_plate)){

            return $this->error('请上传门店门牌照');

        }

        if(empty($business_hours)){

            return $this->error('请填写营业时间');

        }

        if(empty($tel)){

            return $this->error('请填写门店电话');

        }

        if(empty($address)){

            return $this->error('请填写门店地址');

        }

        if(empty($info)){

            return $this->error('请填写门店说明');

        }

        if(empty($lng) || empty($lat)){

            return $this->error('请选择门店定位');

        }



        $this->request->shop->name=$name;

        $this->request->shop->save();



        if(null === $shop_detail = ShopDetailModel::find($this->request->shopId)){

            $shop_detail=new ShopDetailModel();

            $shop_detail->shop_id=$this->request->shopId;

        }

        $pre_logo=$shop_detail->logo;

        $pre_door_license_plate=$shop_detail->door_license_plate;



        $shop_detail->logo=$logo;

        $shop_detail->door_license_plate=$door_license_plate;

        $shop_detail->business_hours=$business_hours;

        $shop_detail->tel=$tel;

        $shop_detail->address=$address;

        $shop_detail->info=$info;

        $shop_detail->lng=$lng;

        $shop_detail->lat=$lat;

        $shop_detail->save();

        cache('miniapp_'.$this->request->shopId,null);

        imgReplaceAct($logo,$pre_logo);

        imgReplaceAct($door_license_plate,$pre_door_license_plate);



        return $this->success('编辑成功');



    }

}

